/***********************************************************/
/*                    WQ Chart Plugin                      */
/*                      Version: 1.0                       */
/*                       肖        鹏       辉                                                                            */
/*                Updated: OUGUST 1th, 2012                */
/***********************************************************/
(function($) {
	$.fn.gdtCharts=function(setting){
		var target = $(this).get(0);
		
		/*默认配置*/
		var defaults = {
			dataUrl:null,
			dataXml:null,
			dataJson:null,
			chartId:'gdtChart',
			width:'100%',
			height:'100%',
			chartType:'Column3D',
			noDataText:"暂时无图表数据！",
			fusionPath:'/DSM/common/plug/FusionCharts'
		};
		
		/*图标类型*/
		var chartTypes = {
			Pie2D:                 '/Charts/Pie2D.swf',
			Pie3D:                 '/Charts/Pie3D.swf',
			Line2D:                '/Charts/Line.swf',
			Bar2D:                 '/Charts/Bar2D.swf',
			Area2D:                '/Charts/Area2D.swf',
			Doughnut2D:            '/Charts/Doughnut2D.swf',
			Doughnut3D:            '/Charts/Doughnut3D.swf',
			Column2D:              '/Charts/Column2D.swf',
			Column3D:              '/Charts/Column3D.swf',
			Pareto2D:              '/Charts/Pareto2D.swf',
			Pareto3D:              '/Charts/Pareto3D.swf',
			MSColumn2D:            '/Charts/MSColumn2D.swf',
			MSColumn3D:            '/Charts/MSColumn3D.swf',
			MSLine2D:              '/Charts/MSLine.swf',
			ZoomLine:              '/Charts/ZoomLine.swf',
			MSArea:                '/Charts/MSArea.swf',
			MSBar2D:               '/Charts/MSBar2D.swf',
			MSBar3D:               '/Charts/MSBar3D.swf',
			Marimekko:             '/Charts/Marimekko.swf',
			StackedColumn2D:       '/Charts/StackedColumn2D.swf',
			StackedColumn3D:       '/Charts/StackedColumn3D.swf',
			StackedColumn3DLineDY: '/Charts/StackedColumn3DLineDY.swf',
			StackedBar2D:          '/Charts/StackedBar2D.swf',
			StackedBar3D:          '/Charts/StackedBar3D.swf',
			MSStackedColumn2D:     '/Charts/MSStackedColumn2D.swf',
			MultiAxisLine:         '/Charts/MultiAxisLine.swf'
		};
		
		//继承插件默认配置		
		setting = $.extend({}, defaults, setting);
		
		/*SWF文件位置*/
		var swfUrl = setting.fusionPath + $(chartTypes).attr(setting.chartType);
		
		//载入Fusionchart
		$.getScript(setting.fusionPath+"/JSClass/FusionCharts.js",function(){
			
			var chart = new FusionCharts(swfUrl , setting.chartId , setting.width , setting.height , 0 , 0);
			
			if(setting.dataUrl != null){
				/*URL转JSON*/
				var param = buildUrl(setting.dataUrl);
				
				/*URL去参数*/
				var wenIndex = setting.dataUrl.indexOf('?');
				if(wenIndex != -1){
					setting.dataUrl = setting.dataUrl.substr(0,wenIndex);
				}
				
				//为避免dataUrl包含中文  本处不使setDataURL API 采用Ajax 请求
				$.ajax({
				   type: "POST",
				   url: setting.dataUrl,
				   cache:false,
				   data: param,
				   async: false,
				   dataType:"text",
				   success: function(_data){
				     chart.setXMLData(_data);
				   }
				});
			}else if(setting.dataXml != null){
				chart.setXMLData(setting.dataXml);
			}else if(setting.dataJson != null){
				chart.setJSONData(setting.dataJson);
			}
			
			chart.configure("ChartNoDataText", defaults.noDataText);
			chart.render(target);
		});
		
		/*链接参数 转 JSON*/
		var buildUrl=function(_curl){
			//获取 ？ 后的参数
			var whindex = _curl.indexOf("?");
			//截取?后所有内容
			if(whindex!=-1){
				var ps = _curl.substr(whindex+1);
				//& 切割成数组
				var _params = ps.split('&');
				//再次切割 = 变成 数组
				var objstr = "{";
				$.each(_params,function(i,_n){
					var oneparamindex = _n.indexOf('=');
					var name = _n.substr(0,oneparamindex);//参数名称
					var value = _n.substr(oneparamindex+1);//参数值
					objstr+="\""+name+"\":"+"\""+value+"\",";
				});
				objstr=objstr.substr(0,objstr.length-1);
				objstr+="}";
				var eobj = eval("("+objstr+")");
				return eobj==undefined?null:eobj;
			}
			return null;
		};
	} ; 
})(jQuery);